$.MENUNAV = new Object();
$.MENUNAV.active = true;
$.MENUNAV.target = $('nav');
$.MENUNAV.contentTarget = '.slides .theImg';
$.MENUNAV.contentID = 'mainContent';
$.MENUNAV.contentIDWrapper = 'mainContentWrapper';
$.MENUNAV.contentTargetRef = '.slides .theImg #' + $.MENUNAV.contentID;

$.MENUNAV.isScrollabeActive = true;
$.MENUNAV.scrollabeMaxH = 0;
$.MENUNAV.scrollabeDelta = 0;
$.MENUNAV.scrollabeMargin = 150;

$.MENUNAV.transitions = true;

$.MENUNAV.init = function () {
    $.MENUNAV.target.find('li').each(function () {
        var currentUrl = $(this).find('a:first').attr('href');
        $(this).find('a:first').removeAttr('href');
        $(this).attr('data-href', currentUrl);
        var currGoto = $(this).attr('goto');
        if (!currGoto) {
            currGoto = '';
        }

        $(this).click(function () {
            if ($.MENUNAV.transitions) {
                $.MENUNAV.transitions = false;
                $.MENUNAV.clear();
                $.MENUNAV.setCurrent($(this));
                $.MENUNAV.loadContent($(this).attr('data-href'));
                $.COLOR.addColorClass($(this).attr('data-color'));
                $.SLIDES.initSound('slideSound', $(this).attr('data-audio'));
                $.SLIDES.initSound('bgSound', $(this).attr('data-audio_bg'));
                window.location.hash = '';
                //window.location.hash = currGoto;
            }
        });
    });


}

$.MENUNAV.open = function () {
    $.MENUNAV.target.slideDown('fast');
}

$.MENUNAV.close = function () {
    $.MENUNAV.target.slideUp('fast');
}

$.MENUNAV.toTop = function () {
    if (!$.MENUNAV.target.hasClass('toTop')) {
        $.MENUNAV.target.slideUp('fast', function () {
            $(this).addClass('toTop');
            $(this).removeClass('toBottom');
            $.MENUNAV.target.slideDown('fast');
        });
    }
}

$.MENUNAV.toBottom = function () {
    if (!$.MENUNAV.target.hasClass('toBottom')) {
        $.MENUNAV.target.slideUp('fast', function () {
            $(this).addClass('toBottom');
            $(this).removeClass('toTop');
            $.MENUNAV.target.slideDown('fast');
        });
    }
}

$.MENUNAV.disattiva = function () {
    $.MENUNAV.active = false;
}

$.MENUNAV.attiva = function () {
    $.MENUNAV.active = true;
}

$.MENUNAV.clear = function () {
    $.MENUNAV.target.find('li').removeClass('current');
}

$.MENUNAV.setCurrent = function ($referrer) {
    $referrer.addClass('current');
}

$.MENUNAV.setCurrentByURL = function ($referrer) {
    $.MENUNAV.target.find('li[data-href="' + $referrer + '"]').addClass('current');
}

$.MENUNAV.loadContent = function ($urlToLoad) {


    $.MENUNAV.clear();
    $.MENUNAV.setCurrentByURL($urlToLoad);
    $.MENUNAV.clearContent();

    if ($urlToLoad == '#' || ($urlToLoad == undefined || $urlToLoad == 'undefined' || $urlToLoad == null)) {
        $('body').removeClass('page');
        $.MENUNAV.toBottom();
        $.MENUNAV.showSticks();

        $.MENUNAV.clearContent();
    } else {
        if (!$('body').hasClass('page')) {
            $('body').addClass('page');
        }
        $.MENUNAV.toTop();
        $.MENUNAV.hideSticks();
        $.MENUNAV.defineContent($urlToLoad);
    }
}

$.MENUNAV.defineContent = function ($urlToLoad) {

    console.log('loading x: ' + $urlToLoad);
    $('body').removeClass('collection2014');

    $('#loader').show();

    if ($urlToLoad != '#' || ($urlToLoad != undefined && $urlToLoad != 'undefined' && $urlToLoad != null)) {

        //mostro - nascondo il livello header nelle collezioni
        if ($urlToLoad == 'ajaxLoad-prodotti.php' || $urlToLoad == 'ajaxLoad-prodotti-2014.php') {
            $('header').css({display: 'none'});
        } else {
            $('header').css({display: 'block'});
        }


        $($.MENUNAV.contentTarget).append('<div id="' + $.MENUNAV.contentID + '" style=""><div id="' + $.MENUNAV.contentIDWrapper + '"></div></div>');

        $urlToLoad = UTILS.noChache($urlToLoad);
        console.log('110 calling for load:' + $urlToLoad);
        $('#' + $.MENUNAV.contentIDWrapper).load($urlToLoad, function (response, status, xhr) {
            if (status == "error") {
                var msg = "Sorry but there was an error: ";
                console.log(msg + xhr.status + " " + xhr.statusText);
                $.MENUNAV.transitions = true;
            } else {
                var the_images = [];
                the_images.push($('.ajaxProvided').attr('data-bg'));
                jQuery.imgpreload(the_images, {each: function () {
                }, all: function () {

                    $('#loader').hide();
                    $('.ajaxProvided').css({'background-image': 'url(' + $('.ajaxProvided').attr('data-bg') + ')'});
                    $.MENUNAV.openContentWrapper();


                }});
            }
        });
    } else {
    }
}

$.MENUNAV.clearContent = function () {
    //tolgo tutti i livelli per tornare alla home
    if ($('#' + $.MENUNAV.contentID).size() > 0) {
        //$($.MENUNAV.contentTargetRef).remove();
        $('#' + $.MENUNAV.contentID).remove();
    }
}

$.MENUNAV.openContentWrapper = function () {
    if ($('.caption').size() > 0) {
        TweenLite.to($('.caption'), .5, {css: {opacity: 0}, ease: Expo.easeOut});
    }
    var refToOpen = $($.MENUNAV.contentTarget).find('#' + $.MENUNAV.contentIDWrapper);
    TweenLite.to(refToOpen, 1, {css: {top: '0%', height: '100%', opacity: 1}, ease: Expo.easeOut, delay: 1.8, onComplete: function () {

        //INIT SCROLL VASRS
        $.MENUNAV.scrollabeMaxH = $('#ajaxContentVrapper').height();
        $.MENUNAV.scrollabeDelta = $('#ajaxContentVrapper').height() - $(window).height();
        if ($('#mainContent .ajaxProvided').hasClass('prodotti')) {

            //seleziono il tipo di layout
            if ($('#mainContent .ajaxProvided').hasClass('prodotti2014')) {
                //LAYOUT PRODOTTI 2014
                console.log('INIT: nuovo layout 2014');
                $.PRODOTTI2014.init();
                TweenLite.to($('#ajaxContent'), .8, {css: {left: '0%'}, ease: Expo.easeOut, onComplete: function () {
                    $.MENUNAV.transitions = true;
                }});
            } else {
                //LAYOUT CLASSICO X PRODOTTI
                $('.ajaxProvided.prodotti #contentProd').height($(window).height() - parseInt($('.ajaxProvided.prodotti #contentProd').css('top')));
                if (!$('body').hasClass('explorer')) {
                    $.PRODOTTI.init();
                }
                TweenLite.to($('#ajaxContent'), .8, {css: {left: '0%'}, ease: Expo.easeOut, onComplete: function () {
                    $.MENUNAV.transitions = true;
                }});
            }

        } else {
            TweenLite.to($('#ajaxContent'), .8, {css: {left: '50%'}, ease: Expo.easeOut, onComplete: function () {
                $.MENUNAV.transitions = true;
            }});
        }
        $.SLIDES.nascondiCaption();
        if ($('#mainContent .caption').size() > 0) {

            $('#mainContent .caption').css('display', 'block');
            $.SLIDES.captionResize();
            TweenLite.to($('#mainContent .caption'), .5, {css: {opacity: 1}, ease: Expo.easeOut});
        }
    }});
}

$.MENUNAV.closeContentWrapper = function () {
    var refToClose = $($.MENUNAV.contentTarget).find('#' + $.MENUNAV.contentIDWrapper);
    TweenLite.to(refToClose, 1, {css: {top: '50%', height: '0%', opacity: 0}, ease: Expo.easeOut, onComplete: function () {
        refToClose.children().remove();
    }});
}


/////SCROLLING

$.MENUNAV.isScrollabe = function () {
    if ($('#ajaxContentVrapper').height() > ($(window).height() - 80)) {
        return true;
    }
    return false;
}


$.MENUNAV.initScroll = function () {
    /*
     $(window).bind('resizeEnd', function(){
     $.MENUNAV.scrollabeMaxH = $('#ajaxContentVrapper').height();
     $.MENUNAV.scrollabeDelta = $('#ajaxContentVrapper').height() - $(window).height();

     if($('#mainContent .ajaxProvided').hasClass('prodotti')){
     $('.ajaxProvided.prodotti #contentProd').height($(window).height() - parseInt($('.ajaxProvided.prodotti #contentProd').css('top')));
     }
     });

     //MOVE EVENT
     $('.slides .pics').on("mousemove", "#ajaxContent.scrollable", function(e){
     //console.log('verify $.MENUNAV.scrollabeMaxH:'+$.MENUNAV.scrollabeMaxH);
     //console.log('verify $.MENUNAV.scrollabeDelta:'+$.MENUNAV.scrollabeDelta);

     if($.MENUNAV.isScrollabe()){

     var cluster = $(window).height() - $.MENUNAV.scrollabeMargin;
     cluster = Math.round($.MENUNAV.scrollabeMaxH/cluster);

     var mouseY = 0;

     if(e.pageY > $.MENUNAV.scrollabeMargin){
     //mouseY = Math.round(Math.abs(e.pageY*($.MENUNAV.scrollabeMaxH+$.MENUNAV.scrollabeMargin)/$.MENUNAV.scrollabeDelta))*-1;
     mouseY = -1*(e.pageY-$.MENUNAV.scrollabeMargin)*cluster;
     }else{
     mouseY = 0
     }

     if(mouseY < (Math.abs($.MENUNAV.scrollabeDelta+$.MENUNAV.scrollabeMargin)*-1)){ mouseY = (Math.abs($.MENUNAV.scrollabeDelta+$.MENUNAV.scrollabeMargin)*-1);}



     TweenLite.to($('#ajaxContentVrapper'), 5, {css:{marginTop:mouseY+'px'}, delay:.1, ease:Expo.easeOut});

     }

     });


     $('.slides .pics').on("mouseleave", "#ajaxContent.scrollable", function() {
     $.MENUNAV.isScrollabeActive = false;
     TweenLite.to($('#ajaxContentVrapper'), 5, {css:{marginTop:'0px'}, delay:.1, ease:Expo.easeOut});
     });

     $('.slides .pics').on("mouseenter", "#ajaxContent.scrollable", function() {
     $.MENUNAV.isScrollabeActive = true;
     });
     */
}


//tablets
$.MENUNAV.initTabs = function () {

    $('.slides .pics section.tabs').hide();
    $('.slides .pics section.tabs.currentTab').slideDown('slow', function () {
        window.myNiceScrollObj.resize();
    });

    $('.slides .pics, #fullUpWrapper').on("mouseover", "#ajaxContent .button", function () {
        $('#ajaxContent .button').not(this).stop().animate({opacity: .5}, 250);
    });

    $('.slides .pics, #fullUpWrapper').on("mouseout", "#ajaxContent .button", function () {
        $('#ajaxContent .button').stop().animate({opacity: 1}, 250);
    });

    $('.slides .pics, #fullUpWrapper').on("click", "#ajaxContent .button", function () {

        var tempCurrent = $(this);
        var tempRelatRef = $(this).attr('data-section');

        //pulisco il menu
        $('.slides .pics .button').removeClass('current');
        tempCurrent.addClass('current');

        //nascondo la tab aperta
        $('.slides .pics section.tabs.currentTab').slideUp('slow', function () {
            //rimuovo la classe
            $(this).removeClass('currentTab');
            $(tempRelatRef).addClass('currentTab');
            $(tempRelatRef).slideDown('slow', function () {
                window.myNiceScrollObj.resize();
            });
        });


        $.MENUNAV.scrollabeMaxH = $('#ajaxContentVrapper').height();
        $.MENUNAV.scrollabeDelta = $('#ajaxContentVrapper').height() - $(window).height();

        window.myNiceScrollObj.resize();
    });
}

$.MENUNAV.theSticks = $('#atari, #atariArea, #subSectionStick, .subSection');

$.MENUNAV.hideSticks = function () {
    TweenLite.to($.MENUNAV.theSticks, .5, {css: {opacity: 0}, delay: .1, ease: Expo.easeOut, onComplete: function () {
        $.MENUNAV.theSticks.hide();
    }});
}
$.MENUNAV.showSticks = function () {

    $.MENUNAV.theSticks.show();
    $.HOMESTICK.openMenu();
    TweenLite.to($.MENUNAV.theSticks, .5, {css: {opacity: 1}, delay: .1, ease: Expo.easeOut, onComplete: function () { /*$.MENUNAV.theSticks.hide();*/
    }});

}

$.MENUNAV.init();
$.MENUNAV.initScroll();
$.MENUNAV.initTabs();